<template>
	<view class="wrap">
		<view class="search">
			<image src="@/static/search.png"></image>
			<input type="text" placeholder="搜索姓名" @input="debounce('getDepartmentDetail',1000)" v-model="searchName" />
		</view>
		<view v-if="isInit" v-cloak>
			<noRecord v-if="menuInfo.list == []" :info="info" v-cloak></noRecord>
			<view class="menu">
				<view class="borderBottom column" v-for="(item, index) in menuInfo.list" :key="index" :class="menuInfo.base.class"
				 @click="chooseItem(index)">
					<view :class="item.chooseKey?'chooseActive':'choose'">&#10004</view>
					<text v-text="item[ele]" v-for="(ele, index) in menuInfo.base.menuProp" :key="index" v-show="item[ele]"></text>
				</view>
			</view>
		</view>
		<view class="btnBox">
			<button class="submitButtonActive" @click="resultApply">确定</button>
		</view>
	</view>
</template>

<script>
	import {
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				departmentId: '',
				menuInfo: {
					base: {
						menuProp: ['name','position']
					},
					list: []
				},
				isInit: false,
				options: "",
				searchName: "",
				timeout:null
			}
		},
		onLoad() {
			this.options = this.$store.state.tipsOptions
		},
		onShow() {
			let pageOptions = this.getPageOptions();
			this.departmentId = pageOptions.id
			this.getDepartmentDetail()
		},
		methods: {
			//搜索防抖
			 debounce(func, wait) {
			        let context = this
			        if (this.timeout) clearTimeout(this.timeout)
			        this.timeout = setTimeout(() => {
			            context[func]()
			        }, wait)
			},
			async getDepartmentDetail() {
				let arr = this.$store.state.idsObj.map(item => {
					return item.id
				})
				let resp = await this.$http.post({
					url: `/oa/teacherInfo/weChat/teacherList`,
					data: {
						departmentId: this.departmentId,
						teacherName: this.searchName
					}
				});
				this.menuInfo.list = resp.result.map(item => {
					if (arr.indexOf(item.id) < 0) {
						return {
							id: item.id,
							jobNumber: item.jobNumber,
							name: item.teacherName,
							position: item.position,
							departmentId: item.departmentId,
							chooseKey: false
						}
					} else {
						return {
							id: item.id,
							jobNumber: item.jobNumber,
							name: item.teacherName,
							position: item.position,
							departmentId: item.departmentId,
							chooseKey: true
						}
					}
				})

				this.isInit = true
			},
			chooseItem(index) {
				if (this.options.singleChoice) {
					this.clearIdsObj()
					this.menuInfo.list.forEach((item, i) => {
						this.menuInfo.list[i].chooseKey = false
					})
					this.menuInfo.list[index].chooseKey = !this.menuInfo.list[index].chooseKey
					this.idsObjIncrease(this.menuInfo.list[index])
				} else {
					this.menuInfo.list[index].chooseKey = !this.menuInfo.list[index].chooseKey
					let allChoose = this.menuInfo.list.every(item => {
						return item.chooseKey
					})
					this.changeTreeChooseStatus({
						id: parseFloat(this.departmentId),
						chooseKey: allChoose
					})
					if (this.menuInfo.list[index].chooseKey) {
						this.idsObjIncrease(this.menuInfo.list[index])
					} else {
						this.idsObjDecrease(this.menuInfo.list[index])
					}
				}
			},
			...mapActions([
				'clearIdsObj',
				'idsObjIncrease',
				'idsObjDecrease',
				'changeTreeChooseStatus'
			]),
			resultApply() {
				let that = this
				// uni.redirectTo({
				// 	url:that.options.originUrl
				// })
				uni.navigateBack({
					delta: 3, // 回退前 delta(默认为1) 页面 
				})

			},
			getList() {
				this.getDepartmentDetail()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search {
		width: 650upx;
		margin: 0 auto 20upx auto;
		border: 1upx solid #ddd;
		height: 60upx;
		border-radius: 30upx;
		position: relative;

		image {
			width: 50upx;
			height: 50upx;
			position: absolute;
			top: 5upx;
			left: 10upx;
		}

		input {
			width: 590upx;
			height: 50upx;
			position: absolute;
			left: 60upx;
			top: 5upx;
			color: #999;
			font-size: 28upx;
		}

		.input-placeholder {
			color: #999;
			font-size: 28upx;
		}
	}

	

	.menu {
		padding: 0 50upx;
		margin-bottom: 120upx;

		.choose {
			width: 30upx;
			height: 30upx;
			border-radius: 50%;
			background: #fff;
			margin: 0 10upx;
			font-size: 0;
			border: 1upx solid #ccc;
		}

		.chooseActive {
			width: 30upx;
			height: 30upx;
			border-radius: 50%;
			font-size: 22upx;
			background: #3ED392;
			color: #fff;
			margin: 0 10upx;
			line-height: 30upx;
			text-align: center;
		}

		.column {
			height: 80upx;
			line-height: 80upx;
			display: flex;
			align-items: center;
			text{
				&:nth-of-type(2){
					margin-left: auto;
				}
			}
		}

		.tit {
			display: flex;
			justify-content: space-between;

			text {
				font-size: 28upx;
				line-height: 100upx;
				font-weight: 600;
			}
		}

		.next {
			margin-left: auto;
			float: right;
		}
	}

	page {
		min-height: 100%;

		.wrap {
			min-height: 100%;
			padding-top: 20upx;
		}

		
	}
	
	.btnBox {
		position: fixed;
		bottom: 0upx;
		left: 50%;
		transform: translateX(-50%);
		width: 100%;
		background: #fff;
		padding: 20upx;
		.submitButtonActive {
			margin: 0 auto;
		}
	}
</style>
